/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FeatureList.java
 *
 * Created on 24-Jun-2010, 09:58:19
 */

package camp.tags;

import camp.anchors.Ship;
import campskeleton.CampSkeleton;
import campskeleton.Plan;
import campskeleton.PlanSkeleton;
import java.awt.Color;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collections;
import javax.swing.DefaultListModel;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.Popup;
import javax.swing.PopupFactory;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import utils.DuckTypeAlphabetComparator;
import utils.WeakListener;

/**
 *
 * @author twak
 */
public class TagListUI extends javax.swing.JPanel {

    Plan plan;

    boolean fireFeatureEvents = true;
    PlanTag tag = null;


    WeakListener.Changed rebuildFeatureList = new WeakListener.Changed() {

        @Override
        public void changed()
        {
            setupTagList( false );
        }
    };


    /** Creates new form FeatureList */
    public TagListUI( Plan ps )
    {
        this.plan = ps;

        initComponents();

        setupTagList( true );
    }

    void removeShip(Ship ship) {
        plan.ships.remove(ship);
        shipOptionsPanel.removeAll();
        setupTagList( true );
    }

    private class TwoString
    {
        String a,b;
        TwoString (String a, String b)
        {
            this.a = a;
            this.b = b;
        }

        @Override
        public String toString() {
            return a;
        }
    }


    private void addTag(String className)
    {
        PlanTag neu = PlanTag.createATag(className);
        plan.tags.add( neu );
        setupTagList( true );
        tagList.setSelectedValue(neu, true);
    }

    void setupTagList( boolean fireEvents )
    {
        if (!fireEvents)
            fireFeatureEvents = false;

        Object o = tagList.getSelectedValue();

        DefaultListModel lm = new DefaultListModel();
        Collections.sort(plan.tags, DuckTypeAlphabetComparator.byToString());

        for ( PlanTag i : plan.tags )
            lm.addElement( i );

        tagList.setModel(lm);

        tagList.setSelectedValue(o, true);

        fireFeatureEvents = true;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        newTagButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tagList = new javax.swing.JList();
        shipOptionsPanel = new javax.swing.JPanel();
        noOptionPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();

        setBorder(null);

        newTagButton.setText("+");
        newTagButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                newTagButtonMousePressed(evt);
            }
        });

        tagList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tagListMouseClicked(evt);
            }
        });
        tagList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                tagListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(tagList);

        shipOptionsPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        shipOptionsPanel.setLayout(new java.awt.GridLayout(1, 0));

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("[no options]");

        javax.swing.GroupLayout noOptionPanelLayout = new javax.swing.GroupLayout(noOptionPanel);
        noOptionPanel.setLayout(noOptionPanelLayout);
        noOptionPanelLayout.setHorizontalGroup(
            noOptionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(noOptionPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)
                .addContainerGap())
        );
        noOptionPanelLayout.setVerticalGroup(
            noOptionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(noOptionPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
                .addContainerGap())
        );

        shipOptionsPanel.add(noOptionPanel);

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("tags");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(newTagButton, javax.swing.GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE)
            .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE)
            .addComponent(shipOptionsPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE)
            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addGap(1, 1, 1)
                .addComponent(newTagButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(shipOptionsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tagListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_tagListValueChanged
        if (fireFeatureEvents)
        {
            tag = (PlanTag) tagList.getSelectedValue();

            shipOptionsPanel.removeAll();
            CampSkeleton.instance.selectedTag = tag;

            if (tag != null)
            {
                JComponent comp = tag.getToolInterface(rebuildFeatureList, plan);
                shipOptionsPanel.add(comp == null ? noOptionPanel : comp);
                shipOptionsPanel.revalidate();
            }
        }
    }//GEN-LAST:event_tagListValueChanged

    private void newTagButtonMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_newTagButtonMousePressed

        DefaultListModel lm = new DefaultListModel();
        for (String s[] : PlanSkeleton.tagTypes) {
            lm.addElement(new TwoString(s[1], s[0]));
        }

        final JList list = new JList(lm);
        list.setBorder(new LineBorder(Color.black, 2));

        Point pt = evt.getPoint();
        SwingUtilities.convertPointToScreen( pt, newTagButton );

        final Popup pop = PopupFactory.getSharedInstance().getPopup( this, list, pt.x, pt.y );

        pop.show();

        list.addMouseListener( new MouseAdapter() {
            @Override
            public void mouseExited( MouseEvent e )
            {
                pop.hide();
            }
        });

        list.getSelectionModel().addListSelectionListener( new ListSelectionListener()
        {
            @Override
            public void valueChanged( ListSelectionEvent e )
            {
                Object o = list.getSelectedValue();
                if (o != null && o instanceof TwoString)
                {
                    // adds to plan!
                    addTag ( ((TwoString)o).b );
                }
                pop.hide();
            }
        });
    }//GEN-LAST:event_newTagButtonMousePressed

    private void tagListMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_tagListMouseClicked
    {//GEN-HEADEREND:event_tagListMouseClicked
//        if (evt.getButton() == MouseEvent.BUTTON3 )
//        {
//             JPopupMenu popup = new JPopupMenu();
//
//            JMenuItem rel = new JMenuItem("clone");
//
//            rel.addActionListener(new ActionListener() {
//
//                @Override
//                public void actionPerformed(ActionEvent e)
//                {
//                    Ship ship = (Ship) tagList.getSelectedValue();
//                    if (ship != null)
//                    {
//                        Ship neu = ship.clone(plan);
//                        plan.ships.add(neu);
//                        setupTagList(true);
//                        tagList.setSelectedValue(neu, true);
//                    }
//                }
//            });
//
//            popup.add(rel);
//
//            rel = new JMenuItem("delete");
//
//            rel.addActionListener(new ActionListener()
//            {
//                @Override
//                public void actionPerformed(ActionEvent e)
//                {
//                    Ship ship = (Ship) tagList.getSelectedValue();
//                    if (ship != null)
//                    {
//                        removeShip(ship);
//                        CampSkeleton.instance.showRoot();
//                    }
//                }
//            });
//            popup.add(rel);
//
//            popup.show(this, evt.getX(), evt.getY()+popup.getPreferredSize().height);
//        }
    }//GEN-LAST:event_tagListMouseClicked



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JButton newTagButton;
    private javax.swing.JPanel noOptionPanel;
    private javax.swing.JPanel shipOptionsPanel;
    private javax.swing.JList tagList;
    // End of variables declaration//GEN-END:variables

}
